package com.gdou.crrp.controller;

import com.gdou.crrp.entity.User;

import com.gdou.crrp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Map;

@SuppressWarnings("ALL")
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //首页跳转
    @RequestMapping("/index")
    public String toIndex(){
        return "index";
    }

    //登录页面跳转
    @RequestMapping("/login")
    public String toLogin(){
        return "user/login";
    }

    //注册页面跳转
    @RequestMapping("/register")
    public String toRegister(){
        return "user/register";
    }

    //无权访问提示页面跳转
    @RequestMapping("/warning")
    public String toWarning(){
        return "user/warning";
    }

    //修改密码确认页面跳转
    @RequestMapping("/password")
    public String toPassword(){
        return "user/password";
    }

    //修改密码页面跳转
    @RequestMapping("/updatePassword")
    public String toUpdatePassword(){
        return "user/updatePassword";
    }

    //实名认证页面跳转
    @RequestMapping("/verify")
    public String toVerify(){
        return "user/verify";
    }

    //用户登录功能
    @PostMapping("/userLogin")
    @ResponseBody
    public String Login(String phone, String password, HttpSession session){

        return userService.login(phone, password, session);
    }

    //发送验证码
    @PostMapping("/sendSms")
    @ResponseBody
    public String sendSms(@RequestParam String phone,@RequestParam long oldtime,HttpSession session) {
        return userService.comparison(phone, oldtime, session);
    }

    //用户注册功能
    @PostMapping("/userRegister")
    @ResponseBody
    public String register(User user,String input_code,HttpSession session) {
        return userService.register(user, input_code, session);
    }

    //显示当前用户信息
    @RequestMapping("/userInfo")
    public String showUserInfo(Model model,HttpSession session){
        String phone = (String) session.getAttribute("phone");
        User user = userService.showUserInfo(phone,session);
        model.addAttribute("users",user);
        System.out.println("当前用户信息："+user);
        return "user/userInfo";
    }

    //修改当前用户信息
    @PostMapping("/updateUserInfo")
    public ModelAndView updateUserInfo(User user){
        String phone = user.getPhone();
        User oldUser = userService.userInfo(phone);
        oldUser.setNickname(user.getNickname());
        oldUser.setGender(user.getGender());
        oldUser.setAge(user.getAge());
        oldUser.setAddress(user.getAddress());
        userService.updateUserInfo(oldUser);
        return new ModelAndView("redirect:userInfo");
    }

    //修改密码，通过手机验证码确认身份
    @PostMapping("/checkcode")
    public String checkcode(String phone,String input_code,HttpSession session){
        String trueCode = (String) session.getAttribute("code");
        String userPhone = (String) session.getAttribute("phone");
        if (phone.equals(userPhone) && input_code.equals(trueCode)){
            return "redirect:updatePassword";
        }
        return "手机号或验证码错误";
    }

    //修改密码
    @PostMapping("/updatePassMethod")
    public String updatePassword(String password,HttpSession session){
        String phone = (String) session.getAttribute("phone");
        User user = userService.userInfo(phone);
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userService.updateUserInfo(user);
        session.removeAttribute("phone");
        return "index";
    }
    //身份验证
    @PostMapping("/userVerify")
    @ResponseBody
    public String userVerify(User user,HttpSession session) throws Exception {
        String phone = (String) session.getAttribute("phone");
        boolean judge = userService.verify(user);
        if (judge == true){
            User presentUser = userService.userInfo(phone);
            presentUser.setName(user.getName());
            presentUser.setIdentity(user.getIdentity());
            userService.updateUserInfo(presentUser);
            return "success";
        }else
        return "failure";
    }
}
